package com.google.code.porntube.snap.api;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
import com.google.code.porntube.snap.core.io.Store;
import com.google.code.porntube.snap.result.Clip;

public final class GetSnap extends HttpServlet {

	private static final long serialVersionUID = -6458137967245986651L;
	
	private static Logger log = Logger.getLogger(GetSnap.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		Clip clip = Store.pop();
		res.setContentType("application/json");
		JSONObject json = new JSONObject();
		if(clip != null) {
			try {
				json.put("id", clip.getId());
				json.put("catalog", clip.getCatalog());
				json.put("duration", clip.getDuration());
				json.put("file", clip.getFile());
				json.put("name", clip.getName());
				json.put("tag", clip.getTag());
				json.put("thumbnail", clip.getThumbnail());
				json.put("update", clip.getUpdate());
				json.put("url", clip.getUrl());
				json.put("config", clip.getConfig());
			} catch (JSONException e) {
				log.log(Level.SEVERE, "GENERATE JSON", e);
			}
		}		
		res.getWriter().println(json);
	}

}
